/*82ZPBNXCeZuAm//ZSTPmZv5h7pt3V6SO8iyesfio7K918wUOCoauQCVq8pdEAgji*/
/*
* 卡片复制
* */
import { ajax, cardCache } from 'nc-lightapp-front';
import ampub from 'ampub';
import constants from '../constants';

const { components: { LoginContext: { loginContextKeys, getContext } }, utils } = ampub;
const { msgUtils: { showMessage, MsgConst, showErrorMessageByRes }, multiLangUtils: { getMultiLangByID } } = utils;
const { URL, UISTATE, assetDataSource, MODAL_ID: { CopyModal: copyCardModal }, AREA: { CARD: { CopyCard }, LIST: { GRID_AREA: tableId } } } = constants;
const copyCardFormId = 'copyCard';

class FaCardCopy {
	constructor(type) {
		this.sourceVo = {};
		this.type = type; //在卡片态复制=“card”, 在列表态复制=“list”
		this.metaItems = {};
	}

	//初始化复制卡片的模板
	initCopyCardTemplate(meta) {
		meta[CopyCard] = {
			code: CopyCard,
			moduletype: 'form',
			name: getMultiLangByID('201201504A-000026') /* 国际化处理： 卡片复制 */,
			items: [
				{
					itemtype: 'number',
					scale: '0',
					col: '12',
					label: getMultiLangByID('201201504A-000027') /* 国际化处理： 请输入数量 */,
					attrcode: 'count',
					isnextrow: true,
					maxlength: '20',
					required: true,
					visible: true,
					min: 1
				},
				{
					itemtype: 'datepicker',
					col: '12',
					label: getMultiLangByID('201201504A-000028') /* 国际化处理： 请确认建卡日期 */,
					attrcode: 'business_date',
					isnextrow: true,
					maxlength: '19',
					required: true,
					visible: true
				}
			]
		};
		meta[CopyCard].status = UISTATE.EDIT;
		if (this.type === 'list') {
			//列表态
			meta[tableId].items.map((item) => {
				this.metaItems[item.attrcode] = item;
			});
		} else {
			//卡片态
		}
	}

	copyCardDialog(that) {
		let { props: { form } } = that;
		return <div>{form.createForm(copyCardFormId)}</div>;
	}

	//显示对话框
	showCopyCardDialog(that, pk_card, sourceVo) {
		let { props: { form, modal } } = that;

		this.sourceVo = sourceVo;
		//form.setFormStatus(copyCardFormId, constants.UISTATE.EDIT);
		let businessDate = getContext(loginContextKeys.businessDate);
		form.setFormItemsValue(copyCardFormId, { business_date: { value: businessDate } });
		form.setFormItemsValue(copyCardFormId, { count: { value: '1' } });
		modal.show(copyCardModal, {
			className: 'senior',
			beSureBtnClick: () => {
				this.doCopy(that, pk_card);
			},
			cancelBtnClick: () => {
				form.EmptyAllFormValue(copyCardFormId); //清空 form
				modal.close(copyCardModal);
			}
		});
	}

	//执行复制
	doCopy(that, pk_card) {
		let { props: { form, modal } } = that;
		//检验必输项
		const checkResult = form.isCheckNow(copyCardFormId);
		if (!checkResult) {
			return false;
		}

		let formData = form.getAllFormValue(copyCardFormId);
		let { rows: [ { values: { business_date, count } } ] } = formData;

		let data = {
			copyBusinessData: business_date.value,
			copyNumber: count.value
		};

		let countReg = /^[1-9]\d*$/;
		if (!countReg.test(data.copyNumber)) {
			/*国际化处理：卡片数量必须为正整数*/
			showMessage(that.props, { content: getMultiLangByID('201201504A-000029'), color: 'danger' });
			return;
		}

		data.pk_card = pk_card;
		ajax({
			url: URL.COPY,
			data,
			success: (res) => {
				let { success, data } = res;
				if (success && data) {
					let { props: { table, form, modal } } = that;
					/*国际化处理：复制成功*/
					showMessage(null, { type: MsgConst.Type.CopySuccess });
					let { allpks, rows } = data[tableId];
					//更新 pk_cardhistory -> clientKeys 缓存
					let pk2ClientKeyMap = cardCache.getDefData(constants.pk2ClientKeyMap, assetDataSource);
					Object.assign(pk2ClientKeyMap, JSON.parse(data.userjson));
					cardCache.setDefData(constants.pk2ClientKeyMap, assetDataSource, pk2ClientKeyMap);

					//更新表格数据
					if (this.type === 'list') {
						let tableData = table.getAllTableData(tableId);
						tableData.rows = tableData.rows.concat(rows);
						table.setAllTableData(tableId, tableData);
					} else {
						//将返回的数据放到列表的缓存中,列表态无需加缓存，平台会给加上
						for (let i = 0; i < allpks.length; i++) {
							cardCache.addCache(allpks[i], null, null, assetDataSource, rows[i].values);
						}
						if (this.successFunc) {
							this.successFunc(allpks[allpks.length - 1]);
						}
					}

					//关闭模态框
					form.EmptyAllFormValue(copyCardFormId); //清空 form
					modal.close(copyCardModal);
				}
			},
			error: (res) => {
				showErrorMessageByRes(that.props, res);
			}
		});
	}

	/**
     * 设置复制成功之后需要执行的函数
     * @param successFunc
     */
	setCopySuccessAfter(successFunc) {
		if (typeof successFunc === 'function') {
			this.successFunc = successFunc;
		}
	}
}

export default FaCardCopy;

/*82ZPBNXCeZuAm//ZSTPmZv5h7pt3V6SO8iyesfio7K918wUOCoauQCVq8pdEAgji*/